package cn.whuc.spark.operator

import org.apache.spark.rdd.RDD
import org.apache.spark.{SparkConf, SparkContext}

object Demo_mapPartitionsWithIndexCase {
  def main(args: Array[String]): Unit = {
    // 1 创建sparkContext
    val sc: SparkContext = new SparkContext(
      new SparkConf()
        .setMaster("local[*]")
        .setAppName(" ")
    )

    // 2 编写代码
    val rdd1: RDD[String] = sc.makeRDD(List("a", "b", "c", "d"), 4)

    val rdd2: RDD[Any] = rdd1.mapPartitionsWithIndex((index, iter) => {
      iter.map((_,index))
    })

    rdd2.collect().foreach(println)
    // 3 关闭上下文对象
    sc.stop()
  }
}
